<!doctype html>
<meta charset="utf-8">
<title>CSSLCH tests</title>
<link rel="help" href="https://drafts.css-houdini.org/css-typed-om-1/#csshwb">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/testhelper.js"></script>
<script>
'use strict';

const gValidLightnessInputs = [
  {lightness: CSS.percent(50), desc: 'percent', expected: CSS.percent(50)},
  {lightness: 0.5, desc: 'number', expected: CSS.percent(50)},
];

const gInvalidLightnessInputs = [
  {lightness: CSS.px(1), desc: "css pixels"},
  {lightness: CSS.deg(180), desc: "degrees"},
]

for (const {lightness, desc, expected} of gValidLightnessInputs) {
  test(() => {
    const result = new CSSLCH(lightness, 0.5, 0.5);
    assert_color_channel_approx_equals(result.l, expected);
  }, `Constructing a CSSLCH with ${desc} for the lightness works as intended.`);

  test(() => {
    const result = new CSSLCH(CSS.percent(0), 0.5, 0.5);
    result.l = lightness;
    assert_color_channel_approx_equals(result.l, expected);
  }, `CSSLCH.l can be updated with a ${desc}.`);
}

for (const {lightness, desc} of gInvalidLightnessInputs) {
  test(() => {
    assert_throws_dom("SyntaxError", () => new CSSLCH(lightness, 0, 0));
  }, `Constructing a CSSLCH with ${desc} for lightness throws a SyntaxError.`);
}

test(() => {
  const result = new CSSLCH(CSS.percent(27), 0.7, 8);
  assert_color_channel_approx_equals(result.l, CSS.percent(27));
  assert_color_channel_approx_equals(result.c, CSS.percent(70));
  assert_color_channel_approx_equals(result.h, CSS.deg(8));
  assert_color_channel_approx_equals(result.alpha, CSS.percent(100));
}, 'CSSLCH can be constructed from three numbers and will get an alpha of 100%.');

test(() => {
  const result = new CSSLCH(0.2, 0.7, CSS.rad(8), CSS.percent(0.4));
  assert_color_channel_approx_equals(result.l, CSS.percent(20));
  assert_color_channel_approx_equals(result.c, CSS.percent(70));
  assert_color_channel_approx_equals(result.h, CSS.rad(8));
  assert_color_channel_approx_equals(result.alpha, CSS.percent(0.4));
}, 'CSSLCH can be constructed from four numbers.');

test(() => {
  assert_throws_dom("SyntaxError", () => new CSSLCH(CSS.number(1), 0, 0, 0));
  assert_throws_dom("SyntaxError", () => new CSSLCH(0, CSS.number(1), 0, 0));
  assert_throws_dom("SyntaxError", () => new CSSLCH(0, 0, CSS.number(1), 0));
  assert_throws_dom("SyntaxError", () => new CSSLCH(0, 0, 0, CSS.number(1)));
}, `Constructing a CSSLCH with CSS.number for l, c, h or alpha throws a SyntaxError`);

for (const attr of ['l', 'c', 'alpha']) {
  test(() => {
    const result = new CSSLCH(CSS.percent(0), 0, 0);
    assert_throws_dom("SyntaxError", () => result[attr] = CSS.number(1));
  }, `Updating a CSSLCH with CSS.number for ${attr} throws a SyntaxError`);

  test(() => {
    const result = new CSSLCH(CSS.percent(0), 0, 0);
    result[attr] = 0.5;
    assert_color_channel_approx_equals(result[attr], CSS.percent(50));
  }, 'CSSLCH.' + attr + ' can be updated to a number.');

  test(() => {
    const result = new CSSLCH(CSS.percent(0), 0, 0);
    result[attr] = CSS.percent(50);
    assert_color_channel_approx_equals(result[attr], CSS.percent(50));
  }, 'CSSLCH.' + attr + ' can be updated with a CSS percent.');
}
</script>
